package cc.iteachyou.template.taglib.directives;

import cc.iteachyou.template.configuration.TemplateEngineConfiguration;
import cc.iteachyou.template.dom.VirtualDOMAttr;
import cc.iteachyou.template.dom.VirtualDOMNode;
import cc.iteachyou.template.taglib.IDirectiveHandler;
import cc.iteachyou.template.taglib.result.DirectiveResult;

import java.util.Map;
import java.util.Optional;

public class HrefDirectiveHandler implements IDirectiveHandler {
    @Override
    public Optional<DirectiveResult> handler(VirtualDOMNode.RendererVirtualDOMAttrContext ctx, Map<String, Object> data) {
        VirtualDOMAttr attr = ctx.getAttr();

        TemplateEngineConfiguration configuration = (TemplateEngineConfiguration) data.get("configuration");


        Object eval = engine.eval(attr.getValue(), data);

        String attrName = attr.getName().replace(configuration.getDirectivePrefix(), "");

        DirectiveResult result = DirectiveResult.builder()
                .name(attrName)
                .value(eval)
                .build();
        return Optional.of(result);
    }
}
